<template>
  <div class="forum-container">
    <el-card>
      <div slot="header" class="forum-header">
        <div class="left-section">
          <span class="title">论坛交流</span>
        </div>
        <div class="right-section">
          <el-button type="primary" @click="showPostDialog">发布新帖子</el-button>
        </div>
      </div>

      <el-tabs v-model="activeTab">
        <el-tab-pane label="最新话题" name="latest">
          <topic-list :topics="topics"></topic-list>
        </el-tab-pane>
        <el-tab-pane label="热门话题" name="hot">
          <topic-list :topics="hotTopics"></topic-list>
        </el-tab-pane>
      </el-tabs>
    </el-card>

    <!-- 发布话题对话框 -->
    <el-dialog title="发布新话题" :visible.sync="postDialogVisible">
      <el-form :model="postForm" :rules="rules" ref="postForm">
        <el-form-item label="标题" prop="title">
          <el-input v-model="postForm.title"></el-input>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <el-input type="textarea" v-model="postForm.content" rows="6"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="postDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitPost">发布</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ForumHome',
  data() {
    return {
      activeTab: 'latest',
      topics: [],
      hotTopics: [],
      postDialogVisible: false,
      postForm: {
        title: '',
        content: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '请输入内容', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    async fetchTopics() {
      try {
        const response = await this.$axios.get('/api/forum/topics')
        this.topics = response.data.data
      } catch (error) {
        this.$message.error('获取话题列表失败')
      }
    },
    async fetchHotTopics() {
      try {
        const response = await this.$axios.get('/api/forum/topics/hot')
        this.hotTopics = response.data.data
      } catch (error) {
        this.$message.error('获取热门话题失败')
      }
    },
    showPostDialog() {
      this.postDialogVisible = true
    },
    async submitPost() {
      try {
        const valid = await this.$refs.postForm.validate()
        if (valid) {
          await this.$axios.post('/api/forum/topics', this.postForm)
          this.$message.success('发布成功')
          this.postDialogVisible = false
          this.fetchTopics()
        }
      } catch (error) {
        this.$message.error('发布失败：' + error.message)
      }
    }
  },
  created() {
    this.fetchTopics()
    this.fetchHotTopics()
  }
}
</script>

<style scoped>
.forum-container {
  padding: 20px;
}

.forum-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 10px;
}

.left-section {
  display: flex;
  align-items: center;
}

.right-section {
  margin-left: auto;
  padding-right: 10px;
}

.title {
  font-size: 16px;
  font-weight: bold;
}
</style> 